package com.example.myapplication

import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    var isNewOp=true
    var dot=false

    fun buCleanEvent(view: View)
    {
        Output.text=""
        isNewOp=true
        dot=false
    }
    private fun TextView.backSpace()
    {
        if(text.isNotEmpty())
        {
            when(text.last())
            {
                '.'->dot=false

            }
            text=text.subSequence(0,text.length-1)
        }
    }

    fun buDelEvent(view: View)
    {
        Output.backSpace()
    }

    fun buPointEvent(view: View)
    {
        Output.append(".")
        dot=true
    }


    fun buNumberEvent(view: View)
    {
        if(isNewOp)
        {
            Output.setText("")
        }
        isNewOp=false
        val buSelect= view as Button
        var buClickValue:String=Output.text.toString()

        when(buSelect.id)
        {
            button0.id->
            {
                buClickValue+="0"
            }
            button1.id->
            {
                buClickValue+="1"
            }
            button2.id->
            {
                buClickValue+="2"
            }
            button3.id->
            {
                buClickValue+="3"
            }
            button4.id->
            {
                buClickValue+="4"
            }
            button5.id->
            {
                buClickValue+="5"
            }
            button6.id->
            {
                buClickValue+="6"
            }
            button7.id->
            {
                buClickValue+="7"
            }
            button8.id->
            {
                buClickValue+="8"
            }
            button9.id->
            {
                buClickValue+="9"
            }

        }
        Output.setText(buClickValue)
    }
    var op="X"
    var oldNumber=""


    fun buOpEvent(view: View)
    {
        val buSelect= view as Button
        when(buSelect.id)
        {
            chengfa.id->
            {
                op="X"
            }
            chufa.id->
            {
                op="÷"
            }
            jianfa.id->
            {
                op="-"
            }
            jiafa.id->
            {
                op="+"
            }
            percent.id->
            {
                op="%"
            }

        }
        oldNumber=Output.text.toString()
        isNewOp=true
        dot=false
    }


    fun buEqualEvent(view: View)
    {
        val newNumber=Output.text.toString()
        var finalNumber:Double?=null
        when(op)
        {
            "X"->
            {
                finalNumber=oldNumber.toDouble() * newNumber.toDouble()
            }
            "÷"->
            {
                finalNumber=oldNumber.toDouble() / newNumber.toDouble()
            }
            "-"->
            {
                finalNumber=oldNumber.toDouble() - newNumber.toDouble()
            }
            "+"->
            {
                finalNumber=oldNumber.toDouble() + newNumber.toDouble()
            }
            "%"->
            {
                finalNumber=oldNumber.toDouble() % newNumber.toDouble()
            }

        }

        Output.setText(oldNumber+op+newNumber+"="+finalNumber)
        isNewOp=true
    }


    fun buPercentEvent(view: View)
    {
        val number=(Output.text.toString().toDouble())/100
        Output.setText(number.toString())
        isNewOp=true
    }



}